package com.estronger.shopmanage.adapter.tasking;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.bean.InstallYPG;
import com.estronger.shopmanage.ui.install.BuildDetailActivity;
import com.estronger.shopmanage.ui.install.pg.InstallPGActivity;
import com.estronger.shopmanage.utils.ConvertUtils;
import com.estronger.shopmanage.utils.PermissionUtils;
import com.estronger.shopmanage.utils.Toast;
import com.estronger.shopmanage.view.AppDialog;

import java.util.ArrayList;

import cn.jesse.nativelogger.NLogger;

import static com.estronger.shopmanage.R.id.install_phone_call;
import static com.estronger.shopmanage.R.id.install_send_sms;

public class InstallYPGListAdapter extends BaseAdapter implements OnClickListener {

    private InstallPGActivity mContext;
    private ArrayList<InstallYPG.Datas.InstallYPGListData> mList;
    public Dialog dialog;

    public InstallYPGListAdapter(InstallPGActivity mContext, ArrayList<InstallYPG.Datas.InstallYPGListData> datas) {
        this.mContext = mContext;
        this.mList = datas;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (mList != null)
            return mList.size();
        else
            return 0;
    }

    @Override
    public InstallYPG.Datas.InstallYPGListData getItem(int position) {
        // TODO Auto-generated method stub
        if (mList == null || mList.size() <= 0)
            return null;
        else
            return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ListItemView ltv;

        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(
                    R.layout.adpter_install_yipai, null);
            ltv = new ListItemView();

            ltv.installsname = (TextView) view.findViewById(R.id.installsname);
            ltv.install_item_location = (TextView) view.findViewById(R.id.install_item_location);
            ltv.install_yytime = (TextView) view.findViewById(R.id.install_yytime);
            ltv.install_shy = (TextView) view.findViewById(R.id.install_shy);
            ltv.install_item_time = (TextView) view.findViewById(R.id.install_item_time);

            ltv.install_phone_call = (ImageView) view.findViewById(R.id.install_phone_call);
            ltv.install_send_sms = (ImageView) view.findViewById(R.id.install_send_sms);

            ltv.install_phone_call.setOnClickListener(this);
            ltv.install_phone_call.setTag(ltv);
            ltv.install_send_sms.setOnClickListener(this);
            ltv.install_send_sms.setTag(ltv);
            ltv.contentRl = view.findViewById(R.id.content_rl);
            view.setTag(ltv);
        } else {
            ltv = (ListItemView) view.getTag();
        }

        try {
            ltv.installsname.setText(ConvertUtils.formatNullF(mList.get(position).cstbuildname) + ConvertUtils.formatNull(mList.get(position).cstname));
            ltv.install_item_location.setText(ConvertUtils.formatNull(mList.get(position).address));
            ltv.install_yytime.setText(mList.get(position).pidate);
            ltv.install_shy.setText(ConvertUtils.formatNull(mList.get(position).installmanname));
            ltv.install_item_time.setText(mList.get(position).istassigndate);
            ltv._phone = mList.get(position).cstmobile;
            ltv.TitelName = ltv.installsname.getText().toString();
            ltv.install_shy.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        int pos = (int) v.getTag(R.id.item_id_p);
                        String name = ConvertUtils.formatNull(mList.get(pos).installmanname);
                        if (!name.equals("")){
                            Toast.show(mContext, name, Toast.LENGTH_LONG);
                        }
                    }catch (Exception e){}

                }
            });
            ltv.contentRl.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    InstallYPG.Datas.InstallYPGListData data = mList.get(position);
                    String dlvid = data.dlvid;
                    boolean isComplesh = false,
                            iftestimony = false;
                    if (data.iststatus.equals("1")) {
                        isComplesh = true;
                        iftestimony = true;
                    }
                    Intent intent = new Intent(mContext, BuildDetailActivity.class);
                    intent.putExtra("dlvid", dlvid);
                    intent.putExtra("isComplesh", isComplesh);
                    intent.putExtra("iftestimony", iftestimony);
                    mContext.startActivity(intent);
                }
            });
            ltv.install_shy.setTag(R.id.item_id_p, position);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return view;
    }

    class ListItemView {
        public TextView installsname, install_item_location, install_yytime, install_shy, install_item_time;
        public ImageView install_phone_call, install_send_sms;
        public String _phone;
        public String TitelName;
        LinearLayout contentRl;
    }

    public String phone;

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case install_phone_call:// 点击了测量条目上的电话小图标
                ListItemView lv = (ListItemView) v.getTag();
                phone = lv._phone;
                if (mContext.isRightNumber(phone)) {
                    dialog = AppDialog.createDialog(mContext,
                            "[" + lv.TitelName + "]", phone, this, "");
                    dialog.show();
                }
                break;
            case install_send_sms:// 点击了测量条目上的发短信小图标
                lv = (ListItemView) v.getTag();
                phone = lv._phone;
                if (mContext.isRightNumber(phone)) {
                    Uri uri = Uri.parse("smsto:" + phone);
                    intent = new Intent(Intent.ACTION_SENDTO, uri);
                    mContext.startActivity(intent);
                }
                break;
            case R.id.ok:
                try {
                    if (PermissionUtils.isCallPhoneGranted(mContext)) {
                        intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
                                + phone));
                        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.
                            return;
                        }
                        mContext.startActivity(intent);
                        dialog.dismiss();
                    } else {
                        PermissionUtils.ToSetPermissions(mContext, R.string.PermissionsCallPhone);
                    }
                } catch (Exception e) {
                    NLogger.e("" + e.getMessage());
                }
                break;
        }
    }
}
